package com.quickask.dao;

import com.quickask.entity.AsknowUserEntity;
import com.quickask.entity.AsknowUserPrivilegeEntity;
import com.quickask.idao.IUserDao;
import com.quickask.operate.data.Constants;
import com.quickask.operate.data.RandomCode;
import com.quickask.operate.data.ResultCode;
import com.quickask.operate.encode.SHA1_Encode;
import com.quickask.operate.sms.SmsVerifyKit;
import org.json.JSONObject;

import java.io.Serializable;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Map;


/**
 * Created by NaSa on 2015/9/3.
 */
public class UserDao extends BaseDao<AsknowUserEntity,Integer> implements IUserDao{

    @Override
    public void doNothing() {
        System.out.println("Hacway!!!doNothing");
    }

    /**
     * 查看手机号码是否已被注册过
     * @param phoneNum 手机号码
     * @return 返回结果码，详见com.quickask.operate.data.ResultCode.java
     */
    @Override
    public int isPhoneExist(String phoneNum) {
        long count = countByHql("select count(*) from AsknowUserEntity as u where u.userPhone=?",phoneNum);
        if(count>0) {
            return ResultCode.USERPHONE_REGISTERED;
        }
        return ResultCode.SUCCESS;
    }

    /**
     * 查看账号是否已被注册过
     * @param userAccount 账号
     * @return 返回结果码，详见com.quickask.operate.data.ResultCode.java
     */
    @Override
    public int isAccountExist(String userAccount) {
        long count = countByHql("select count(*) from AsknowUserEntity as u where u.userAccount=?",userAccount);
        if(count>0) {
            return ResultCode.USERACCOUNT_REGISTERED;
        }
        return ResultCode.SUCCESS;
    }

    /**
     * 通过手机注册
     * @param phoneNum 手机号码
     * @param passwordNo 密码
     * @param zone 区号，中国为86
     * @param code 短信验证码
     * @return
     */
    @Override
    public int registerByPhone(String phoneNum,String passwordNo, String zone, String code) {
        if(isPhoneExist(phoneNum) == ResultCode.USERPHONE_REGISTERED) {  //该手机号已被注册过
            return ResultCode.USERPHONE_REGISTERED;
        }
        int status;
        SmsVerifyKit smsVerifyKit = new SmsVerifyKit(Constants.SHARE_SDK_SMS_APPKEY,phoneNum,zone,code);
        try {
            JSONObject jsonObject = new JSONObject(smsVerifyKit.checkCode());
            status = (Integer)jsonObject.get("status");
            switch (status) {
                case ResultCode.SMS_200: //验证成功
                    AsknowUserEntity asknowUserEntity = new AsknowUserEntity();
                    asknowUserEntity.setUserPhone(phoneNum);
                    asknowUserEntity.setUserPassword(passwordNo);
                    asknowUserEntity.setUserPhoneZoneCode(zone);
                    asknowUserEntity.setUserCreateDate(new RandomCode().generateCurrentTimestamp());
                    asknowUserEntity.setUserToken(new SHA1_Encode().getDigestOfString((phoneNum + passwordNo).getBytes()));
                    save(asknowUserEntity);break;
            }
            return status; //返回验证请求验证的结果码
        } catch (Exception e) {
            return ResultCode.UNKNOWN_ERROR; //未知错误
        }
    }

    /**
     * 通过QQ注册
     * @param qqId QQ的唯一识别码
     * @return 返回结果码，详见com.quickask.operate.data.ResultCode.java
     */
    @Override
    public int registerByQQ(String qqId) {
        return 0;
    }

    /**
     * 通过微信注册
     * @param wechatId
     * @return 返回结果码，详见com.quickask.operate.data.ResultCode.java
     */
    @Override
    public int registerByWechat(String wechatId) {
        return 0;
    }

    /**
     * 普通注册
     * @param account 账号
     * @param passwordNo 密码
     * @return 返回结果码，详见com.quickask.operate.data.ResultCode.java
     */
    @Override
    public int registerByNormal(String account, String passwordNo) {
        if(isAccountExist(account) == ResultCode.USERACCOUNT_REGISTERED) {
            return ResultCode.USERACCOUNT_REGISTERED;
        }

        AsknowUserEntity asknowUserEntity = new AsknowUserEntity();
        asknowUserEntity.setUserAccount(account);
        asknowUserEntity.setUserPassword(passwordNo);
        asknowUserEntity.setUserCreateDate(new RandomCode().generateCurrentTimestamp());
        asknowUserEntity.setUserToken(new SHA1_Encode().getDigestOfString((account+passwordNo).getBytes()));
        save(asknowUserEntity);
        return ResultCode.SUCCESS;
    }

    /**
     * 通过手机号码登录
     * @param phoneNum 手机号码
     * @param passwordNo 密码
     * @return 返回用户实体，包含用户的主要信息
     */
    @Override
    public AsknowUserEntity loginByPhone(String phoneNum, String passwordNo) {
        AsknowUserEntity asknowUserEntity = getByHQL("from AsknowUserEntity as u where u.userPhone=? and u.userPassword=?",phoneNum,passwordNo);
        return asknowUserEntity;
    }

    /**
     * 通过QQ登录
     * @param qqId
     * @return 返回用户实体，包含用户的主要信息
     */
    @Override
    public AsknowUserEntity loginByQQ(String qqId) {
        AsknowUserEntity asknowUserEntity =  getByHQL("from AsknowUserEntity as u where u.userQq=?",qqId);
        return asknowUserEntity;
    }

    /**
     * 通过微信登录
     * @param wechatId
     * @return 返回用户实体，包含用户的主要信息
     */
    @Override
    public AsknowUserEntity loginByWechat(String wechatId) {
        AsknowUserEntity asknowUserEntity =  getByHQL("from AsknowUserEntity as u where u.wechatId=?",wechatId);
        return asknowUserEntity;
    }

    /**
     * 通过账号登录
     * @param account 账号
     * @param passwordNo 密码
     * @return 返回用户实体，包含用户的主要信息
     */
    @Override
    public AsknowUserEntity loginByNormal(String account, String passwordNo) {
        AsknowUserEntity asknowUserEntity = getByHQL("from AsknowUserEntity as u where u.userAccount=? and u.userPassword=?",account,passwordNo);
        return asknowUserEntity;
    }

    /**
     * 用户token是否存在
     * @param userId
     * @param token
     * @return 更新后的refreshToken
     */
    @Override
    public int isTokenExist(Integer userId, String token) {
        AsknowUserEntity asknowUserEntity = get(userId);
        if(asknowUserEntity == null) {
            return ResultCode.USERID_ERROR;
        }
        if(!asknowUserEntity.getUserToken().equals(token)) {
            return ResultCode.USER_TOKEN_ERROR;
        }
        return ResultCode.SUCCESS;
    }

    @Override
    public String loadToken(Integer userId) {
        AsknowUserEntity asknowUserEntity = get(userId);
        if(null == asknowUserEntity) {
            return null;
        }
        return asknowUserEntity.getUserToken();
    }


}
